#include "usermodel.h"
#include <QDebug>



UserModel::UserModel()
{

}

//静态变量的初始化必须定义在外面
UserModel* UserModel::singleObj = NULL;
QMap<int, responseUserInfo> UserModel::friendListData;

UserModel* UserModel::NewInstance(){
    if(singleObj == NULL){
        singleObj = new UserModel();
    }

    return singleObj;
}

//登陆api
bool UserModel::login(QString account, QString password, int userStatus){
    requestLoginStruct login;
    login.route = RequestStruct::REQUEST_API_LOGIN;
    login.uid = account.toInt();
    login.password = password.toStdString();
    login.userStatus = userStatus;

    QJsonObject jsonRoot, jsonData;
    jsonData.insert("uid", login.uid);
    jsonData.insert("password", password);

    jsonRoot.insert("route", QString::fromStdString(login.route));
    jsonRoot.insert("data", jsonData);

    std::string jsonStrStd = this->convertQJsonObjToString(jsonRoot);

//    QJsonDocument jsonDoc;
//    jsonDoc.setObject(jsonRoot);
//    QByteArray byteArray = jsonDoc.toJson(QJsonDocument::Compact);
//    QString jsonStr(byteArray);
//    std::string jsonStrStd = jsonStr.toStdString();

    //发送请求，执行登陆
    if(!this->net->sendMsg(jsonStrStd)){
        qDebug() << "发送失败，请检查网络或者服务器未开启" << endl;
        return false;
    }

    //登陆成功后设置uid
//    this->setUid(login.uid);

    return true;
}


//获取个人信息
bool UserModel::getProfile(int uid){
    requestGetUserInfo req;
    req.route = RequestStruct::XIAOH_REQUEST_API_GET_USER_INFO;
    req.uid = uid;

    QJsonObject jsonRoot, jsonData;
    jsonData.insert("uid", req.uid);
    jsonRoot.insert("route", QString::fromStdString(req.route));
    jsonRoot.insert("data", jsonData);

    std::string jsonStrStd = this->convertQJsonObjToString(jsonRoot);

    //发送请求
    if(!this->net->sendMsg(jsonStrStd)){
        qDebug() << "发送失败，请检查网络或者服务器未开启" << endl;
        return false;
    }

    return false;
}

//获取好友列表
bool UserModel::getFriendList(requestFriendListStruct req){
    req.route = RequestStruct::REQUEST_API_FRIEND_LIST;

    QJsonObject jsonRoot, jsonData;
    jsonData.insert("uid", req.uid);
    jsonRoot.insert("route", QString::fromStdString(req.route));
    jsonRoot.insert("data", jsonData);
    QJsonDocument jsonDoc;
    jsonDoc.setObject(jsonRoot);
    QByteArray byteArray = jsonDoc.toJson(QJsonDocument::Compact);
    QString jsonStr(byteArray);
    std::string jsonStrStd = jsonStr.toStdString();

    //发送请求
    if(!this->net->sendMsg(jsonStrStd)){
        qDebug() << "发送失败，请检查网络或者服务器未开启" << endl;
        return false;
    }

    return true;
}

/**
 * @brief UserModel::sendMsg发送消息
 * @param req
 * @return
 */
bool UserModel::sendMsg(requestChatStruct req){
    req.route = RequestStruct::REQUEST_API_CHAT;

    QJsonObject jsonRoot, jsonData;
    jsonData.insert("uid", req.uid);
    jsonData.insert("beuid", req.beuid);
    jsonData.insert("msg", QString::fromStdString(req.msg));
    jsonRoot.insert("route", QString::fromStdString(req.route));
    jsonRoot.insert("data", jsonData);

    std::string jsonStrStd = this->convertQJsonObjToString(jsonRoot);

    //发送请求
    if(!this->net->sendMsg(jsonStrStd)){
        qDebug() << "发送失败，请检查网络或者服务器未开启" << endl;
        return false;
    }

    return true;
}

void UserModel::setFriendListData(int uid, responseUserInfo user){
    if(this->friendListData.contains(uid)){
       this->friendListData[uid] = user;
    }else{
        this->friendListData.insert(uid, user);
    }
}

responseUserInfo UserModel::getFriendDataByUid(int uid){
    if(this->friendListData.contains(uid)){
        return this->friendListData[uid];
    }

    responseUserInfo u;
    return u;
}

QMap<int, responseUserInfo> UserModel::getFriendListData(){
    return this->friendListData;
}

//获取好友列表待废除
QVector<FriendListItem> UserModel::getFriendList(){
    QVector<FriendListItem> result;
    UserProfile testProfile;
    testProfile.uid = 1;
    testProfile.avatar = "";
    testProfile.sex = ResponseStruct::USER_SEX_MAN;
    testProfile.status = ResponseStruct::USER_STATUS_ONLINE;
    testProfile.statusText = "[online]";
    testProfile.nickname = "测试账号1";
    testProfile.signText = "这场比赛一定要拿下哟";

    UserProfile testProfile2;
    testProfile2.uid = 100;
    testProfile2.avatar = "";
    testProfile2.sex = ResponseStruct::USER_SEX_MAN;
    testProfile2.status = ResponseStruct::USER_STATUS_ONLINE;
    testProfile2.statusText = "[online]";
    testProfile2.nickname = "测试账号2";
    testProfile2.signText = "这场比赛一定要拿下哟";

    UserProfile testProfile3;
    testProfile3.uid = 200;
    testProfile3.avatar = "";
    testProfile3.sex = ResponseStruct::USER_SEX_MAN;
    testProfile3.status = ResponseStruct::USER_STATUS_ONLINE;
    testProfile3.statusText = "[online]";
    testProfile3.nickname = "测试账号3";
    testProfile3.signText = "这场比赛一定要拿下哟";

    QVector<UserProfile> profileList;

    //test data
    FriendListItem item1;
    item1.name = "我的好友";
    item1.onlineNum = 2;
    item1.totalNum = 3;
//    for (int i= 0; i < item1.totalNum; i++ ){
//        testProfile.uid += 1;
//        profileList.append(testProfile);
//        qDebug()<<testProfile.uid<<endl;
//    }
    profileList.append(testProfile);
    profileList.append(testProfile2);
    profileList.append(testProfile3);
    item1.friendList = profileList;

    QVector<UserProfile> profileList2;
    FriendListItem item2;
    item2.name = "南葛队";
    item2.onlineNum = 1;
    item2.totalNum = 3;
    for (int i= 0; i < item2.totalNum; i++ ){
        testProfile.uid += 1;
        profileList2.append(testProfile);
        qDebug()<<testProfile.uid<<endl;
    }
    item2.friendList = profileList2;

    QVector<UserProfile> profileList3;
    FriendListItem item3;
    item3.name = "教练组";
    item3.onlineNum = 0;
    item3.totalNum = 1;
    for (int i= 0; i < item3.totalNum; i++ ){
        testProfile.uid += 1;
        profileList3.append(testProfile);
        qDebug()<<testProfile.uid<<endl;
    }
    item3.friendList = profileList3;

    result.append(item1);
    result.append(item2);
    result.append(item3);

    return result;
}
